iT邦幫忙

2025 iThome 鐵人賽

DAY 27
0
Modern Web

Laravel 是甚麼系列 第 27

通知按鈕

  • 分享至 

  • xImage
  •  

修改後>按聯絡我們會報錯,因為共用通知
https://ithelp.ithome.com.tw/upload/images/20251006/20119035GRePtgGPhg.png
顯示
https://ithelp.ithome.com.tw/upload/images/20251006/20119035DlMB7AI9hS.png
程式碼

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Product;
use App\Models\User;

class WebController extends Controller
{
    public function index()
    {
        $products =Product::all();
        $user=User::find(3);
        $notifications = $user->notifications ?? [];
        return view('web.index',['products' =>$products, 'notifications' =>$notifications]);
    }

    public function contactUs()
    {
        return view('web.contact_us');
}


修改程式碼

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Product;
use App\Models\User;
use Illuminate\Notifications\DatabaseNotification;

class WebController extends Controller
{
    public $notifications = [];
    public function __construct()
    {
        $user=User::find(3);
        $this->notifications = $user->notifications ?? [];
    }

    public function index()
    {
        $products =Product::all();
       
        return view('web.index',['products' =>$products, 'notifications' =>$this->notifications]);
    }

    public function contactUs()
    {
        return view('web.contact_us', ['notifications' =>$this->notifications]);
    }
    
    public function readNotification(Request $request)
    {
        $id = $request->all()['id'];
        DatabaseNotification::find($id)->markAsRead();

        return response(['result'=>true]);
    }

}

https://ithelp.ithome.com.tw/upload/images/20251006/20119035ydbjKeNJum.png
資料表14/15/16
https://ithelp.ithome.com.tw/upload/images/20251006/20119035St00rTyMWR.png
用POSTMAN的POST去通知
https://ithelp.ithome.com.tw/upload/images/20251006/20119035kXNEeor3f8.png
+
https://ithelp.ithome.com.tw/upload/images/20251006/20119035ekWguB0pW1.pnghttps://ithelp.ithome.com.tw/upload/images/20251006/20119035phGVMnc0kF.png

資料表看到
https://ithelp.ithome.com.tw/upload/images/20251006/201190358jlP0xb32n.png
改成user都是3
https://ithelp.ithome.com.tw/upload/images/20251006/20119035S8BD20UbhF.png
就可以看到4個通知
https://ithelp.ithome.com.tw/upload/images/20251006/20119035x8owiE0GiC.png

https://ithelp.ithome.com.tw/upload/images/20251006/20119035lX6T6lxbMC.png
修正程式碼
https://ithelp.ithome.com.tw/upload/images/20251006/20119035aWcsheCfeK.png
確認數量的按鈕修好
https://ithelp.ithome.com.tw/upload/images/20251006/20119035bx8DojSKlG.png
修改前端程式碼

  <ul>
      @foreach ($notifications as $notification)
          <li class="read_notification" data-id="{{ $notification->id }}">{{ $notification->data['msg'] }} 
            <span class="read">
              @if ($notification->read_at)
                  (已讀)
              @endif
            </span>
          </li>
      @endforeach
    </ul>
  </div>
</div>

https://ithelp.ithome.com.tw/upload/images/20251006/20119035s21RJkQu55.png


設定路由
https://ithelp.ithome.com.tw/upload/images/20251006/20119035TUhMlY0D37.png
畫面上面按加入_ 看到已讀
https://ithelp.ithome.com.tw/upload/images/20251006/20119035ZGJjcEsqK5.png

資料表加上read_at欄位就是紀錄是否 被讀過欄位
https://ithelp.ithome.com.tw/upload/images/20251006/20119035oIsMmDgueb.png
被讀過欄位
https://ithelp.ithome.com.tw/upload/images/20251006/20119035vFlP6fdUFv.png

大家明天見~


上一篇
讀檔
下一篇
上傳圖片
系列文
Laravel 是甚麼30
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言